package com.ittest.controller;

import com.github.pagehelper.Page;
import com.ittest.common.Code;
import com.ittest.common.PageResult;
import com.ittest.common.Result;
import com.ittest.pojo.Employee;
import com.ittest.service.DeptService;
import com.ittest.service.EmployeeService;
import com.sun.media.sound.SoftTuning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工表现层
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 根据部门id带分页查询所有员工信息
     * @param page 当前页
     * @param size 每页条数
     * @return
     */
    @GetMapping("/{dept_id}/{page}/{size}")
    public Result<Employee> getEmps(
            @PathVariable("dept_id") Integer dept_id,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size){
        Page<Employee> emps = employeeService.getEmps(dept_id, page, size);
        PageResult<Employee> pageResult = new PageResult<>(emps.getTotal(), emps.getPages(), emps.getResult());
        return new Result(Code.OK,"根据部门id查询成功",pageResult);
    }



    /**
     * 根据id删除某个员工信息
     * @param id 员工id
     * @return
     */
    @DeleteMapping("{id}")
    public Result<Employee> delete(@PathVariable("id") Integer id){
        boolean delete = employeeService.delete(id);
        if (delete){
            return new Result<Employee>(Code.OK,"删除成功！");
        }else{
            return new Result<Employee>(Code.ERROR,"删除失败！");
        }
    }


    /**
     * 修改员工操作
     * @param employee
     * @return
     */
    @PutMapping
    public Result<Employee> update(@RequestBody Employee employee){
        boolean result = employeeService.update(employee);
        if (result){
            return new Result<Employee>(Code.OK,"修改员工信息，成功！");
        }else{
            return new Result<Employee>(Code.ERROR,"修改员工信息，失败！");
        }
    }

    /**
     * 新增操作
     */
    @PostMapping
    public Result<Employee> add(@RequestBody Employee employee){
        boolean result = employeeService.add(employee);
        if (result){
            return new Result<Employee>(Code.OK,"添加成功！");
        }else{
            return new Result<Employee>(Code.ERROR,"新增失败！");
        }
    }


    /**
     * 登录验证
     */
    @PostMapping("login")
    public Result<Employee> login(String userName, String password){
        Employee employee = employeeService.login(userName,password);
        Result<Employee> employeeResult = null;
        if (employee != null){
            employeeResult = new Result<Employee>(Code.OK, "登录成功！",employee);
        }else{
            employeeResult = new Result<Employee>(Code.ERROR, "登陆失败！",employee);
        }
        return employeeResult;
    }

}
